/*
 * Copyright (c) 2003-2006 jMonkeyEngine, 2007 Aleksey Nikiforov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.lex.input.mouse;

import com.jme.input.InputHandler;
import com.jme.input.Mouse;
import com.jme.input.MouseInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.math.Vector2f;

/**
 * This is a copy of AbsoluteMouse class with some methods removed, some added,
 * and update method fixed.
 * 
 * @author lex
 *
 */
public class AnotherAbsoluteMouse extends Mouse {

    private static final long serialVersionUID = 1L;

    private float width;
    private float height;
    
    private boolean active;
	private Vector2f absolute = new Vector2f();
    
    private MouseInput mouseInput;
    
    /**
     * While it is possible to disable mouse updates by registering
     * it with a null InputHandler, doing so will require registering
     * the mouse again to enable it. The InputHandler.accAction() method
     * creates new objects every time it is called. This mouse is supposed
     * to be constantly activated and deactivated, so instead of
     * or unregistering it, simple "if(!active)" check is added.
     */
    private InputAction updateAction = new InputAction() {
        public void performAction(InputActionEvent evt) {
        	if (!active) return;
        	
        	hotSpotLocation.x += mouseInput.getXDelta()*speed;
            if (hotSpotLocation.x < 0) {
            	hotSpotLocation.x = 0;
            } else if (hotSpotLocation.x > width - 1) {
            	hotSpotLocation.x = width - 1;
            }
            localTranslation.x = hotSpotLocation.x - hotSpotOffset.x;
            worldTranslation.x = localTranslation.x;
            absolute.x = hotSpotLocation.x;

        	hotSpotLocation.y += mouseInput.getYDelta()*speed;
            if (hotSpotLocation.y < 0) {
                hotSpotLocation.y = 0;
            }
            else if (hotSpotLocation.y > height - 1) {
                hotSpotLocation.y = height - 1;
            }
            localTranslation.y = hotSpotLocation.y - hotSpotOffset.y;
            worldTranslation.y = localTranslation.y;
            absolute.y = hotSpotLocation.y;
        }
    };

    private InputHandler currentHandler;

    /**
     * Constructor instantiates a new <code>AbsoluteMouse</code> object. The
     * limits of the mouse movements are provided.
     *
     * @param name   the name of the scene element. This is required for
     *               identification and comparision purposes.
     * @param width  the width of the mouse's limit.
     * @param height the height of the mouse's limit.
     */
    public AnotherAbsoluteMouse(String name, float width, float height) {
        super(name);
        this.width = width;
        this.height = height;
        updateAction.setSpeed(1);
        
        mouseInput = MouseInput.get();
    }

    /**
     * set the mouse's limit.
     *
     * @param width  the width of the mouse's limit.
     * @param height the height of the mouse's limit.
     */
    public void setLimit( float width, float height ) {
        this.width = width;
        this.height = height;
    }

    public void setSpeed(float speed) {
        updateAction.setSpeed(speed);
    }
    
    public float getSpeed() {
    	return updateAction.getSpeed();
    }

    /**
     * Registers the xUpdateAction and the yUpdateAction with axis 0 and 1 of the mouse.
     * Note: you can register the actions with other devices, too, instead of calling this method.
     * @param inputHandler handler to register with (null to unregister)
     * @see #getXUpdateAction()
     * @see #getYUpdateAction()
     */
    public void registerWithInputHandler( InputHandler inputHandler ) {
        if ( currentHandler != null ) {
            currentHandler.removeAction(updateAction);
        }
        currentHandler = inputHandler;
        
        if ( inputHandler != null ) {
            inputHandler.addAction(updateAction,
            	InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_NONE, 0, true);
        }
    }
    
    public boolean isActive() {
    	return active;
    }
    
    public void setActive(boolean active) {
    	this.active = active;
    }

    public void setHotSpotPosition(float x, float y) {
		hotSpotLocation.set(x, y, 0);
		localTranslation.x = hotSpotLocation.x - hotSpotOffset.x;
		localTranslation.y = hotSpotLocation.y - hotSpotOffset.y;
		
		absolute.x = hotSpotLocation.x;
		absolute.y = hotSpotLocation.y;
		
		worldTranslation.set(localTranslation);
	}
    
    public Vector2f getMousePosition() {
    	return absolute;
    }
    
    /**
     * @return the height
     */
    public float getHeight() {
        return height;
    }

    /**
     * @return the width
     */
    public float getWidth() {
        return width;
    }
    
}
